package com.autotest.glin.run;

import com.autotest.glin.common.RunCaseParams;
import com.autotest.glin.data.DataProviders;
import com.alibaba.fastjson.JSONObject;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

public class JsonDemoRunner {
    @BeforeClass
    public void setUp() {
        RestAssured.baseURI = "http://www.baidu.com";
        RestAssured.basePath = "/s";
        RestAssured.port = 80;
    }

    @Test(dataProvider = "dataprovider", dataProviderClass = DataProviders.class, description = "runJsonNormalCases")
    public void runCase(String caseMeta, String params) {
        Response response = RunCaseParams.runCase(params, "get");
        response.prettyPrint();

        String jsonStr = response.asString();
        jsonStr = "{\"jmxfilename\":\"newdemo111.jmx\",\"proName\":\"demo\",\"debugmode\":false,\"testplan\":{\"arguments\":[{\"name\":\"data_dir\",\"value\":\"/neworiental/cgl/xdf_pt/xdf_pt_data\"}],\"threadgrouplist\":[{\"threads\":\"1\",\"ramptime\":\"1\",\"scheduler\":\"true\",\"loops\":\"-1\",\"duration\":\"1\",\"httpsamplers\":[{\"testname\":\"liang1_hteee\",\"domain\":\"www.baidu1.com\",\"port\":\"80\",\"protocol\":\"http\",\"path\":\"/\",\"method\":\"GET\",\"connect_timeout\":\"0\",\"response_timeout\":\"0\",\"keepalive\":\"true\",\"paraType\":\"parameters\",\"clearcache\":true,\"clearcookie\":true,\"patterns\":[{\"key\":\"ee\"}],\"testType\":33},{\"testname\":\"dsfds\",\"domain\":\"www.baidu.com\",\"port\":\"80\",\"protocol\":\"http\",\"path\":\"/\",\"method\":\"GET\",\"connect_timeout\":\"0\",\"response_timeout\":\"0\",\"keepalive\":\"true\",\"paraType\":\"parameters\",\"clearcache\":true,\"clearcookie\":true}]}]}}";
        JSONObject resJson = JSONObject.parseObject(jsonStr);
        Assert.assertEquals(resJson.getString("jmxfilename"), "newdemo111.jmx");
        JSONObject testplan = resJson.getJSONObject("testplan");
        List<JSONObject> threadgroupList = new ArrayList<JSONObject>();
        threadgroupList = (List<JSONObject>) testplan.get("threadgrouplist");
        for (JSONObject threadgroup : threadgroupList) {
            List<JSONObject> httpsamplerList = new ArrayList<JSONObject>();
            httpsamplerList = (List<JSONObject>) threadgroup.get("httpsamplers");
            for (JSONObject httpsampler : httpsamplerList) {
                Assert.assertEquals(httpsampler.getString("protocol"), "http");
            }
        }
    }
}
